<?php

use Symfony\Component\Filesystem\Filesystem;

 class EduSohoUpgrade extends AbstractUpdater
 {
     public function update()
     {
        $this->getConnection()->beginTransaction();
        try{
            $this->updateScheme();
            $this->installRouting();
            $this->installAssets();

            $this->createService('System.SettingService')->set('coupon', array(
              'enabled'=> 1,
            ));


            $this->getConnection()->commit();
        } catch(\Exception $e) {
            $this->getConnection()->rollback();
            throw $e;
        }
     }

    public function installRouting()
    {
        $config = <<<EOT


coupon_web:
    resource: "@CouponBundle/Resources/config/routing.yml"
    prefix:   /

coupon_admin:
    resource: "@CouponBundle/Resources/config/routing_admin.yml"
    prefix:   /admin
EOT;
        $filepath = $this->kernel->getParameter('kernel.root_dir') . '/config/routing_plugins.yml';
        file_put_contents($filepath, $config, FILE_APPEND);
    }

    private function installAssets()
    {
        $rootDir = realpath($this->kernel->getParameter('kernel.root_dir') . '/../');

        $originDir = "{$rootDir}/plugins/Coupon/CouponBundle/Resources/public";
        $targetDir = "{$rootDir}/web/bundles/coupon";

        $filesystem = new Filesystem();

        if ($filesystem->exists($targetDir)) {
            $filesystem->remove($targetDir);
        }

        $filesystem->mirror($originDir, $targetDir, null, array('override' => true, 'delete' => true));
    }


     private function updateScheme()
     {
        $connection = $this->getConnection();

        $connection->exec("DROP TABLE IF EXISTS `coupon`;");
        $connection->exec("
            CREATE TABLE `coupon` (
              `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
              `code` varchar(255) NOT NULL COMMENT '优惠码',
              `type` enum('minus','discount') NOT NULL COMMENT '优惠方式',
              `status` enum('used','unused') NOT NULL COMMENT '使用状态',
              `rate` float(10,2) unsigned NOT NULL COMMENT '若优惠方式为打折，则为打折率，若为抵价，则为抵价金额',
              `batchId` int(10) unsigned NOT NULL COMMENT '批次号',
              `userId` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用者',
              `deadline` int(10) unsigned NOT NULL COMMENT '失效时间',
              `targetType` varchar(64) NOT NULL COMMENT '使用对象类型',
              `targetId` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用对象',
              `orderId` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '订单号',
              `orderTime` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用时间',
              `createdTime` int(10) unsigned NOT NULL,
              PRIMARY KEY (`id`)
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='优惠码表';");

        $connection->exec("DROP TABLE IF EXISTS `coupon_batch`;");
        $connection->exec("
            CREATE TABLE `coupon_batch` (
              `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
              `name` varchar(64) NOT NULL COMMENT '批次名称',
              `type` enum('minus','discount') NOT NULL COMMENT '优惠方式',
              `generatedNum` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '生成数',
              `usedNum` int(11) NOT NULL DEFAULT '0' COMMENT '使用次数',
              `rate` float(10,2) unsigned NOT NULL COMMENT '若优惠方式为打折，则为打折率，若为抵价，则为抵价金额',
              `prefix` varchar(64) NOT NULL COMMENT '批次前缀',
              `digits` int(20) unsigned NOT NULL COMMENT '优惠码位数',
              `money` float(10,2) NOT NULL DEFAULT '0.00' COMMENT '已优惠金额',
              `deadline` int(10) unsigned NOT NULL COMMENT '失效时间',
              `targetType` varchar(64) NOT NULL DEFAULT '' COMMENT '使用对象类型',
              `targetId` int(10) unsigned NOT NULL DEFAULT '0',
              `description` text COMMENT '优惠说明',
              `createdTime` int(10) unsigned NOT NULL,
              PRIMARY KEY (`id`)
            ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='优惠码批次表';
        ");
     }

 }


 abstract class AbstractUpdater
 {
    protected $kernel;
    public function __construct ($kernel)
    {
        $this->kernel = $kernel;
    }

    public function getConnection()
    {
        return $this->kernel->getConnection();
    }

    protected function createService($name)
    {
        return $this->kernel->createService($name);
    }

    protected function createDao($name)
    {
        return $this->kernel->createDao($name);
    }

    abstract public function update();
   
 }
